package com.aiti.lulian.controller;

import cn.hutool.core.date.DateUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardZfFileDto;
import com.aiti.lulian.entity.CardZfFile;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.service.ICardZfFileService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/card_zf_file")
public class CardZfFileController extends WebController<CardZfFile> {

    private ICardZfFileService baseService;

    private ILuLianEnterpriseUserService enterpriseUserService;

    public CardZfFileController(ICardZfFileService baseService,ILuLianEnterpriseUserService enterpriseUserService) {
        this.baseService = baseService;
        this.enterpriseUserService = enterpriseUserService;
    }

    @GetMapping("/getCardFileList")
    public Message getCardFileList(CardZfFileDto dto){

        String enterpriseId = dto.getEnterpriseId();
        if(StringUtils.isBlank(dto.getEnterpriseId())){
            LuLianEnterprise luLianEnterprise = enterpriseUserService.selectCurrentUserJoinEnterpriseInfo(findUserId());
            if(luLianEnterprise == null || StringUtils.isBlank(luLianEnterprise.getBaseId())){
                return Message.fail("没有找到绑定的企业信息");
            }
            enterpriseId = luLianEnterprise.getBaseId();
        }

        dto.setEnterpriseId(enterpriseId);
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        Page<CardZfFile> cardZfFilePage = startPage(pageNum, pageSize);
        LambdaQueryWrapper<CardZfFile> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CardZfFile::getEnterpriseId,dto.getEnterpriseId());
        Page<CardZfFile> page = baseService.page(cardZfFilePage, lqw);
        return Message.success(page);
    }


    @PostMapping("/saveCardZfFile")
    public Message saveCardZfFile(@RequestBody CardZfFile cardZfFile){
        String userId = findUserId();
        LuLianEnterprise luLianEnterprise = enterpriseUserService.selectCurrentUserJoinEnterpriseInfo(userId);

        if(luLianEnterprise == null || StringUtils.isBlank(luLianEnterprise.getBaseId())){
            return Message.fail("没有找到绑定的企业信息");
        }
        cardZfFile.setEnterpriseId(luLianEnterprise.getBaseId());
        cardZfFile.setCardId(userId);
        cardZfFile.setBaseCreateTime(DateUtil.date());
        cardZfFile.setBaseCreateBy(userId);
        baseService.save(cardZfFile);
        return Message.success();
    }

    @PostMapping("/deleteCardZfFile")
    public Message deleteCardZfFile(@RequestBody CardZfFile cardZfFile){
        baseService.removeById(cardZfFile.getBaseId());
        return Message.success();
    }
}
